package com.example.contactapp;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Groups;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.contactapp.adapters.FragmentAdapter;
import com.example.contactapp.fragments.ChatFragment;
import com.example.contactapp.fragments.ContactFragment;

public class MainActivity extends BaseActivity implements OnPageChangeListener,OnClickListener{

	private Button mBtnContact, mBtnChat;
	private ViewPager mViewPager;
	private FragmentAdapter mAdapter;
	private ArrayList<Fragment> mFragments;
	private int mCurIndex = 0;
	private CountDownTimer cDT;
	private boolean ready = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
		initListeners();
		initProperties();
	}

	@Override
	protected void initViews() {
		mBtnContact = (Button)findViewById(R.id.btn_contact);
		mBtnChat = (Button)findViewById(R.id.btn_chat);
		
		mViewPager = (ViewPager)findViewById(R.id.viewpager);
	}

	@Override
	protected void initListeners() {
		mBtnContact.setOnClickListener(this);
		mBtnChat.setOnClickListener(this);
		
		mViewPager.setOnPageChangeListener(this);
	}

	@Override
	protected void initProperties() {
//		checkGroup();
		mFragments = new ArrayList<>();
		mFragments.add(new ContactFragment());
		mFragments.add(new ChatFragment());
		
		mAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragments);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setCurrentItem(0);
		mBtnContact.setSelected(true);
		
		cDT = new CountDownTimer(3000, 3000) {

			@Override
			public void onTick(long arg0) {
			}

			@Override
			public void onFinish() {
				ready = false;
			}
		};
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		if(arg0 == 0){
			mBtnContact.setSelected(true);
			mBtnChat.setSelected(false);
		}else{
			mBtnContact.setSelected(false);
			mBtnChat.setSelected(true);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_contact:
			mViewPager.setCurrentItem(0);
			break;

		case R.id.btn_chat:
			mViewPager.setCurrentItem(1);
			break;
		}
	}
	
	public void checkGroup(){
		String[] arrGroupName = getResources().getStringArray(R.array.arr_group);
		for (int i = 1; i < arrGroupName.length; i++) {
			if (getGroupId(arrGroupName[i]) == null) {
				createGroup(arrGroupName[i]);
			}
		}
	}
	
	private String getGroupId(String groupTitle) {
		String selection = Groups.GROUP_VISIBLE + "=?";
		Cursor cursor = getContentResolver().query(Groups.CONTENT_URI, null,
				selection, new String[]{"1"}, null);
		cursor.moveToFirst();
		int len = cursor.getCount();

		String groupId = null;
		for (int i = 0; i < len; i++) {
			String id = cursor.getString(cursor.getColumnIndex(Groups._ID));
			String title = cursor
					.getString(cursor.getColumnIndex(Groups.TITLE));

			if (title.equals(groupTitle)) {
				groupId = id;
				break;
			}
			cursor.moveToNext();
		}
		cursor.close();

		return groupId;
	}
	
	private void createGroup(String title) {
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

		ops.add(ContentProviderOperation
				.newInsert(ContactsContract.Groups.CONTENT_URI)
				.withValue(ContactsContract.Groups.TITLE, title)
				.withValue(ContactsContract.Groups.GROUP_VISIBLE, 1).build());
		try {

			getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

		} catch (Exception e) {
			Log.e("Error", e.toString());
		}

	}
	
	@Override
	public void onBackPressed() {
		if (ready) {
			super.onBackPressed();
		} else {
			ready = true;
			Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
			cDT.start();
		}
	}
}
